// packageA/pages/allotWrap/detail/detail.js
const app = getApp()
import customerApi from "../../../../api/index.js"
import hasPermission from "../../../../utils/auth"
let submit = false
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		showShadow1: false,
		showStatus: false,
		selectData: [],
		showGroup: false,
		showShadow: false,
		showContent: false,
		showSuccess: false,
		showSuccessText: '分配成功',
		allot_group: false,
		allot_user: false,
		add_follow: false,
		contact_phone: false,
		self_allot: false,
         
		update_follow: false,
		edit_customer: false,
		update_customer_status: false,
		repeat_allot:false,
		backgourd: '#097c25',
		showSee: false,
		color: '',
		group_type: '',
		panyuan: [],
		follow_status: [{
				id: '1',
				name: '重点跟进'
			},
			{
				id: '2',
				name: '可跟进'
			},
			{
				id: '3',
				name: '无效'
			},
      {
        id: '4',
        name: '暂缓'
      },
      {
        id: '5',
        name: '已租'
      },

		],
		follow_status_id: 0,
		customer_id: '',
		showItem: {},
		group_id: '',
		department: '',
		select_group_id: '',
		details: [],
		content: '',
		allData: [{
				label: '服装/手袋',
				value: 1
			},
			{
				label: '机械、器械类',
				value: 1
			},
			{
				label: '塑料、塑胶类',
				value: 1
			},
			{
				label: '玻璃制品类',
				value: 1
			},
			{
				label: '家具、家私类',
				value: 1
			},
			{
				label: '重工业类',
				value: 1
			},
			{
				label: '五金制品类',
				value: 1
			},
			{
				label: '电子、电器类',
				value: 1
			},
			{
				label: '木制品类',
				value: 1
			}
		]
	},
	callBtn: function(e) {
		wx.makePhoneCall({
			phoneNumber: this.data.details.mobiles[e.currentTarget.dataset.index],
			success: res => {

			},
			fail: res => {
				console.log('fail', res)
			}
		})
	},
	editCustomer: function(e) {
		wx.navigateTo({
				url: '/packageA/pages/add/add?id=' + e.currentTarget.dataset.id
			}
		)
	},
  updateHome() {
    // 从首页过来 && 验证倒数第二个页面栈为首页(当前页的上一页）
    let pages = getCurrentPages();
    let l = pages.length;
    let home = pages[l - 2];
    console.log(home)
    if (home) {
      home.eventUser(this.data.customer_id)
    }
  },
	getSelect: function (e) {
	  console.log(e)
	  customerApi.getSecondDepartUser({groupIds:e.detail}).then(
	    res=>{
	      console.log(res)
	      console.log(res.data.data)
	
	      let child = this.selectComponent('#Usertanchuang') 
	      child.setData({
	        selectData: [],
	        showShadow: true,
	        group_type: 0,
	        showGroup: true,
	        isFixed: true,
		    	single:true,
	        color: this.data.color,
	        list: res.data
	      })
	    }
	  )
	},
	/**
	 * 选择user
	 */
	getSelectUser:function(e){
	  let userIds=e.detail
	  console.log(userIds)

    wx.showLoading({
      title: '请求数据中...',
    })
    customerApi.repeatShareUserCustomer({
      customer_id: this.data.customer_id,
      ower_user_id: userIds[0]
    }).then(res => {
      this.setData({
        showShadow: false,
        showGroup: false,
        showSee: false,
        showSuccess: true,
        showSuccessText: '提交成功',
      })  
      setTimeout(() => {
        this.setData({
          showSuccess: false,
          showShadow: false,
        })
      }, 1500)
      this.updateHome()
      wx.hideLoading()
      this.reLoad()
    })
	},
	
	
	closeBtn1: function() {
		this.setData({
			showShadow1: false,
			showStatus: false,

		})
	},
	callstatus: function() {
		if (this.data.showShadow1) {
			this.setData({
				showShadow1: false,
				showStatus: false
			})
		} else {
			this.setData({
				showShadow1: true,
				showStatus: true
			})
		}
	},
	input: function(e) {
		console.log(e)
		this.setData({
			content: e.detail.value
		})
	},
	seeContent: function(e) {
		console.log(e)
		this.setData({
			showSee: true,
			showShadow: true,
			showItem: this.data.details.records[e.currentTarget.dataset.index]
		})
	},
	updateRecord: function(e) {
		if (submit == true) {
			return
		}
		submit = true
		wx.showLoading({
			title: '提交中',
		})
		console.log(e)
		customerApi.followUpdate({
			follow_id: e.currentTarget.dataset.id,
			desc: this.data.content
		}).then(res => {
			wx.hideLoading()
			submit = false
			this.setData({
				showShadow: false,
				showGroup: false,
				showSee: false,
				showSuccess: true,
				showSuccessText: '提交成功',
			})
			setTimeout(() => {
				this.setData({
					showSuccess: false,
					showSuccessText: '提交成功',
				})
				customerApi.getCustomerDetails({
					customer_id: this.data.details.customer_id
				}).then(res => {
					if (res.code == 200) {
						this.setData({
							details: res.res
						})
					}
				})
			}, 2000)
		})
	},
	submitRecord: function(e) {

		if (submit == true) {
			return
		}
		submit = true
		wx.showLoading({
			title: '提交中',
		})
		customerApi.setCustomerRecords({
			customer_id: this.data.details.customer_id,
			desc: this.data.content
		}).then(res => {
			wx.hideLoading()
			submit = false
			this.setData({
				showContent: false,
				showShadow: false,
				content: '',
				showSuccess: true,
				showSuccessText: '提交成功',
			})
			setTimeout(() => {
				this.setData({
					showSuccess: false,
					showSuccessText: '提交成功',
				})
				customerApi.getCustomerDetails({
					customer_id: this.data.details.customer_id
				}).then(res => {
					if (res.code == 200) {
						this.setData({
							details: res.res
						})
					}
				})
			}, 2000)
		})
	},
	updateStatus: function(e) {
 
   
    this.setData({
      follow_status_id: e.currentTarget.dataset.id,
     
    })
   
	},
	submitStatus: function(e) {
		customerApi.submitStatus({
			customer_id: this.data.details.customer_id,
			follow_status_id: this.data.follow_status_id
		}).then(res => {
      let color = '#cd1e1f'
      if (this.data.follow_status_id == 1) {
        color = "#cd1e1f"
      } else {
        color = '#097c25'
      }
			this.setData({
         color:color, 
				showShadow1: false,
				showStatus: false,


			})
     
			wx.showToast({
				title: '更改成功',
			})
      this.updateHome()
		})
	},
	chongxinfenpei:function(e){

		
		  let child = this.selectComponent('#tanchuang') 
			console.log(child)
		      child.setData({
		        allotCur: e.currentTarget.dataset.id,
		        showShadow: true,
		        group_type:0,
		        showGroup: true,
		        isFixed: true,
		    		single:true,
		        color:this.data.color,
		        list:this.data.department
		      })
		
	},
	selectBtn: function(e) {
		if (this.data.group_type == 0 || this.data.group_type == 2) {
			if (e.currentTarget.dataset.index == this.data.selectData) {
				this.setData({
					selectData: '',
					group_id: ''
				})
			} else {
				this.setData({
					selectData: e.currentTarget.dataset.index,

				})
			}
		}
		if (this.data.group_type == 1) {
			if (e.currentTarget.dataset.index == this.data.selectData) {
				this.setData({
					selectData: '',
					select_group_id: ''
				})
			} else {
				this.setData({
					selectData: e.currentTarget.dataset.index,
					select_group_id: e.currentTarget.dataset.index
				})
			}
		}
	},
	allotBtn: function(e) {

		if (e.currentTarget.dataset.index == 1) {
			this.setData({
				showShadow: true,
				showGroup: true,
				isFixed: true,
				group_type: e.currentTarget.dataset.index,
				customer_id: e.currentTarget.dataset.id,

			})
			wx.showLoading({
				title: '加载中',
				mask: true,
				success: function(res) {},
				fail: function(res) {},
				complete: function(res) {},
			})
			customerApi.getDepartments({
				customer_id: e.currentTarget.dataset.id
			}).then(res => {

				this.setData({
					showData: res.res
				})
				wx.hideLoading()
			})
		}
		if (e.currentTarget.dataset.index == 2) {
			this.setData({
				showShadow: true,
				showGroup: true,
				isFixed: true,
				group_type: e.currentTarget.dataset.index,
				customer_id: e.currentTarget.dataset.id,
			})
			customerApi.getUsers({
				customer_id: this.data.customer_id
			}).then(res => {


				this.setData({
					user: res.data,

					showData: res.data
				})
			})

		}
	},

	submitBtn: function(e) {

		//筛选结果
		if (this.data.group_type == 0) {
			this.setData({
				showGroup: false,
				allData: [],
				page: 1,
				showShadow: false,
				isFixed: true,
				group_id: this.data.selectData
			})
			this.reLoad()
			return
		}
		if (this.data.group_type == 2) {
			wx.showLoading({
				title: '请求数据中...',
			})
			customerApi.shareUserCustomer({
				ower_user_id: this.data.selectData,
				customer_id: this.data.customer_id
			}).then(res => {
				this.setData({
					showShadow: true,
					showSuccess: true,
					showGroup: false,
					isFixed: true,
					allData: [],
					selectData: '',
					page: 1
				})
				setTimeout(() => {
					this.setData({
						showSuccess: false,
						showShadow: false,
					})
				}, 1500)
				wx.hideLoading()
        this.updateHome()
				this.reLoad()
   
			})


			return
		}
		if (this.data.group_type == 1) {
			wx.showLoading({
				title: '请求数据中...',
			})
			customerApi.shareGroupCustomer({
				customer_id: this.data.customer_id,
				group_id: this.data.select_group_id
			}).then(res => {
				this.setData({
					showShadow: true,
					showSuccess: true,
					showGroup: false,
					isFixed: true,
					selectData: '',
					allData: [],
					page: 1
				})
				setTimeout(() => {
					this.setData({
						showSuccess: false,
						showShadow: false,
					})
				}, 1500)
        this.updateHome()
				wx.hideLoading()
				this.reLoad()
			})


			return
		}

	},
	selfBtn: function(e) {

		wx.showModal({
			title: '确定跟进',
			content: '确定自己跟进',
			success: res => {
				if (res.confirm) {
					customerApi.shareUserMe({
						customer_id: this.data.details.customer_id
					}).then(res => {
						this.setData({
							showShadow: true,
							showSuccess: true,
							showGroup: false,
							isFixed: true,
							selectData: '',
							allData: [],
							page: 1
						})
						wx.hideLoading()
            this.updateHome()
						this.reLoad()
						return
					})
				} else if (res.cancel) {

					console.log('用户点击取消')
				}
			}
		})
	},
	closeBtn: function() {
		this.setData({
			showShadow: false,
			showGroup: false,
			showSuccess: false,
			showContent: false,
			showSee: false,
			showShadow: false
		})
	},
	contentBtn: function() {
		this.setData({
			showContent: true,
			showShadow: true
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		app.CheckSession().then(res => {
			if (res == true) {
				app.GetUser().then(data => {
					this.setData({
						userInfo: data,
            customer_id:options.customer_id,
						allot_group: hasPermission('allot_group'),
						allot_user: hasPermission('allot_user'),
						self_allot: hasPermission('self_allot'),
						add_follow: hasPermission('add_follow'),
						update_follow: hasPermission('update_follow'),
						contact_phone: hasPermission('contact_phone'),
						update_customer_status: hasPermission('update_customer_status'),
						edit_customer: hasPermission('edit_customer'),
						repeat_allot: hasPermission('repeat_allot')
					})

					customerApi.getDepartments().then(res => {
						this.setData({
							department: res.res
						})
					})

					var customer_id = options.customer_id
					customerApi.recommdPanyuan({
						id: customer_id
					}).then(
						res => {
							this.setData({
								panyuan: res.data
							})

						}
					)
					customerApi.getCustomerDetails({
						customer_id: customer_id
					}).then(res => {
						let color=''
						if (res.code == 200) {
							if(res.res.status==2){
								 color='#097c25'
							}else{
								 color='#cd1e1f'
							}
              if (res.res.follow_status == 1 || res.res.follow_status == 0){
								 color='#cd1e1f'
							}
							this.setData({
								details: res.res,
								color:color
							})
													
						}
					})
				})
			}
		})
	},
	reLoad() {
		customerApi.getCustomerDetails({
			customer_id: this.data.details.customer_id
		}).then(res => {
					let color=''
			if (res.code == 200) {
				if(res.res.status==2){
					 color='#097c25'
				}else{
					 color='#cd1e1f'
				}
        if (res.res.follow_status == 1 || res.res.follow_status == 0){
					 color='#cd1e1f'
				}
				this.setData({
					details: res.res,
						color:color
				})
			}
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},
	ToDetail(e) {
		wx.navigateTo({
			url: '/pages/detail/detail?id=' + e.currentTarget.dataset.id,
			success: function(res) {},
			fail: function(res) {},
			complete: function(res) {},
		})
	}
})
